<template>
    <div>
        <div style="margin-bottom: 30px;">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item to="{path:'/'}">首页</el-breadcrumb-item>
              <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div style="padding: 10px 0;">
            <el-input style="width: 200px;"  placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
            <el-input style="width: 200px;"  placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>
            <el-input style="width: 200px;"  placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0;">
            <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm
              class="ml-5"
              confirmButtonText='确定'
              cancelButtonText='再想想'
              icon="el-icon-info"
              iconColor="red"
              title="这些内容确定批量删除吗？"
              @confirm="delBatch"
            >
              <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            <el-upload action="http://localhost:8080/user/import" :show-file-list="false" accept="xlsx" :on-success="handleImportSuccess" style="display: inline-block;">
              <el-button type="primary" class="ml-5" @click="imp" >导入<i class="el-icon-bottom"></i></el-button>
            </el-upload>
            
            <el-button type="primary" class="ml-5" @click="exp" >导出<i class="el-icon-top"></i></el-button>
        </div>

        <el-table :data="tableData" border stripe header-cell-class-name="headerBg"  @selection-change="handleSelectionChange">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="140">
            </el-table-column>
            <el-table-column prop="nickname" label="昵称" width="120">
            </el-table-column>
            <el-table-column prop="role" label="权限角色" width="120">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column prop="phone" label="电话">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>

            <el-table-column label="操作">
              <!-- <template slot-scope="scope"> -->
              <template slot-scope="scope">
                <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
                <el-popconfirm
                  class="ml-5"
                  confirmButtonText='确定'
                  cancelButtonText='再想想'
                  icon="el-icon-info"
                  iconColor="red"
                  title="这一段内容确定删除吗？"
                  @confirm="del(scope.row.id)"
                >
                  <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>               
              </template>
            </el-table-column>
              
        </el-table>
        <div style="padding: 10px 0;">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </div>
        <!-- 对话框（增加与修改） -->
        <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width="80px" size="small">
              <el-form-item label="用户名">
                <el-input v-model="form.username" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="昵称">
                <el-input v-model="form.nickname" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="权限角色">
                <el-select clearable v-model="form.role" placeholder="请选择角色" style="width: 100%">
                  <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
                </el-select>
              </el-form-item>
              <!-- <el-form-item label="权限角色 ">
                <el-select clearable v-model="entity.name" placeholder="请选择角色" style="width: 100%;">
                  <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
                </el-select>
              </el-form-item> -->
              <el-form-item label="邮箱">
                <el-input v-model="form.email" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="form.phone" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="地址">
                <el-input v-model="form.address" auto-complete="off"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible=false">取消</el-button>
              <el-button type="primary" @click="save">确定</el-button>
            </div>
        </el-dialog>
    </div>
     
</template>


<script>
    import request from '@/utils/request';
    export default{
        name:"UserView",
        data() {
        
            return {
                tableData:[],
                total:0,
                pageNum:1, //当前页码
                pageSize:5, //一页的大小
                username:"",
                nickname:"",
                email:"",
                phone:"",
                address:"",
                form:{}, //对话框内容
                dialogFormVisible:false, //对话框可视化，默认不可视
                multipleSelection:[],
                roles:[]
            }
        },

        created(){
        //加载初始数据
            this.load()
        },

        // components:{
        //   AsideMenu,
        //   HeaderMenu,
        // },

        methods:{
            collapse(){
                this.isCollapse=!this.isCollapse
                if(this.isCollapse){ //收缩
                this.sideWith=64
                this.collapseBtnClass='el-icon-s-unfold'
                this.logoTextShow=false;
                }
                else{ //展开
                this.sideWith=200
                this.collapseBtnClass='el-icon-s-fold'
                this.logoTextShow=true
                }
        } ,

        load(){
            request.get("/user/page",{
                params:{
                    pageNum:this.pageNum,
                    pageSize:this.pageSize,
                    username:this.username,
                    email:this.email,
                    address:this.address,
            }
            }).then(res =>{
                this.tableData=res.records
                this.total=res.total
            }),

            request.get("/role").then(res =>{
              this.roles=res.data;
            })
        } ,
        reset(){
            this.username=""
            this.email=""
            this.address=""
            this.load()
        },

        save(){ //保存新数据
          
            request.get("/user/username/"+this.form.username).then(res=>{
                if(res.data !=null){
                    this.$message.error("用户名已存在")
                    return false
                }else if(this.submitForm() === false){
                    return false
                }else{
                  request.post("/user",this.form).then(res => {
                      if(res){
                          this.$message.success("保存成功")
                          this.dialogFormVisible=false
                          this.load()
                      }else{
                          this.$message.error("保存失败")
                      }
                  })
                }
            })

            // request.post("/user",this.form).then(res => {
            //     if(res){
            //         this.$message.success("保存成功")
            //         this.dialogFormVisible=false
            //         this.load()
            //     }else{
            //         this.$message.error("保存失败")
            //     }
            // })
        },

         // 验证电话
         submitForm() {
            const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
            const phoneRegex = /^1[3-9]\d{9}$/; // 美国电话号码格式
            if (!emailRegex.test(this.form.email)) {
              this.$message.error("邮箱格式错误")
              return false;
            }
            if (!phoneRegex.test(this.form.phone)) {
              this.$message.error("电话号码格式错误")
              return false;
            }
          },

        handleSizeChange(pageSize){
            console.log(pageSize)
            this.pageSize=pageSize
            this.load()
        },
        handleCurrentChange(pageNum){
            console.log(pageNum)
            this.pageNum=pageNum
            this.load()
        },
        handleAdd(){ //新增数据
            this.dialogFormVisible=true
            this.form={}
        }, 
        handleEdit(row){ //编辑
            this.form=row
            this.dialogFormVisible=true
        },
        del(id){ //删除
            request.delete("/user/"+id).then(res => {
            if(res){
                this.$message.success("删除成功")
                this.load()
            }else{
                this.$message.error("删除失败")
            }
            })
        },
        handleSelectionChange(val){ //批量选中
            console.log(val)
            this.multipleSelection=val
        },
        delBatch(){
            let ids = this.multipleSelection.map(v => v.id) //[{},{},{}]=>[1,2,3]
            request.post("/user/del/batch",ids).then(res => {
                if(res){
                    this.$message.success("批量删除成功")
                    this.load()
                }else{
                    this.$message.error("批量删除失败")
                }
            })
        },

       

        exp(){
          window.open("http://localhost:8080/user/export")
        },
        handleImportSuccess(){
          this.$message.success("导入成功")
          this.load()
        }

    }
    };
</script>


<style>
  .el-header {
    /* background-color: #B3C0D1; */
    color: #333;
    line-height: 60px;
  }
  
  .el-aside {
    color: #333;
  }

  .headerBg{
    background: #eee !important;
  }

</style>